<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_daftar_keluarga extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_keluarga';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$id_keluarga = isset($_POST['id_keluarga']) ? strval($_POST['id_keluarga']) : '';
		$dusun = isset($_POST['dusun']) ? strval($_POST['dusun']) : '';
		$rt = isset($_POST['rt']) ? strval($_POST['rt']) : '';
		$rw = isset($_POST['rw']) ? strval($_POST['rw']) : '';
		$ekonomi = isset($_POST['ekonomi']) ? strval($_POST['ekonomi']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.*,b.nama, c.nama_desa');
			$this->db->from('keluarga a');
			$this->db->join('warga b', 'b.no_ktp = a.kepala_keluarga');
			$this->db->join('desa c', 'c.id_desa = a.dusun');
			
			#Kimochi - otoritas
				#--Faklutas
			/* if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			*/

			if($id_keluarga != ''){
				$this->db->like('a.id_keluarga', $id_keluarga, 'both');
			}
			
			if($dusun != ''){
				$this->db->where('a.dusun', $dusun);
			}
			
			if($rt != ''){
				$this->db->like('a.rt', $rt, 'both');
			}
			
			if($rw != ''){
				$this->db->like('a.rw', $rw, 'both');
			}
			
			if($ekonomi != ''){
				$this->db->where('a.ekonomi', $ekonomi);
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getDataEdit($id_keluarga){
		$this->db->flush_cache();
		$this->db->select('a.*, b.nama as nama_kk');
		$this->db->from('keluarga a');
		$this->db->join('warga b', 'a.kepala_keluarga = b.no_ktp');
	
		$this->db->where('a.id_keluarga', $id_keluarga);
		return $this->db->get();
	}
	
	function getDataAnggota($id_keluarga){
		$this->db->flush_cache();	
		$this->db->where('id_keluarga', $id_keluarga);
		return $this->db->get('warga');
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('keluarga');
		
		return $this->db->get()->num_rows();
	}
	
	function getDataChart($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('thn_daftar', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('no_ktp',$kode);
		
		$this->db->select('*');
		$this->db->from('warga');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		
		//echo $data['anggota_keluarga'];
		
		//query insert data
		$this->db->trans_start();

		$this->db->flush_cache();
		$this->db->set('id_keluarga', $data['id_keluarga']);
		$this->db->set('kepala_keluarga', $data['kepala_keluarga']);
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('dusun', $data['dusun']);
		$this->db->set('rt', $data['rt']);
		$this->db->set('rw', $data['rw']);
		$this->db->set('ekonomi', $data['ekonomi']);
		$this->db->set('thn_daftar', $data['thn_daftar']);
		//$this->db->set('anggota', $data['anggota_keluarga']);
		//$this->db->set('status', $data['status']);

		$result = $this->db->insert('keluarga');
		
		#update table warga
		$anggota_keluarga = json_decode($data['anggota_keluarga']);
		foreach ($anggota_keluarga->rows as $row) {
			# code...
			$this->db->flush_cache();
			$this->db->set('id_keluarga', $data['id_keluarga']);
			$this->db->where('no_ktp', $row->no_ktp);
			$this->db->update('warga');
		}
		
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}

	function UpdateOnDb($data){
		//query insert data
		$this->db->trans_start();

		// $this->db->flush_cache();
		// $this->db->set('kepala_keluarga', $data['kepala_keluarga']);
		// $this->db->set('alamat', $data['alamat']);
		// $this->db->set('dusun', $data['dusun']);
		// $this->db->set('rt', $data['rt']);
		// $this->db->set('rw', $data['rw']);
		// $this->db->set('ekonomi', $data['ekonomi']);
		// $this->db->set('thn_daftar', $data['thn_daftar']);
		// //$this->db->set('anggota', $data['anggota_keluarga']);
		// //$this->db->set('status', $data['status']);
		// $this->db->where('id_keluarga', $data['id_keluarga']);

		// $result = $this->db->update('keluarga');
		
		#update table warga
		$anggota_keluarga = json_decode($data['anggota_keluarga']);
		foreach ($anggota_keluarga->rows as $row) {
			# code...
			$this->db->flush_cache();
			$this->db->set('id_keluarga', $data['id_keluarga']);
			$this->db->where('no_ktp', $row->no_ktp);
			$this->db->update('warga');
		}
		
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}

	function getdatadetail($id_keluarga){
		$this->db->flush_cache();
		$this->db->select('a.*, b.nama as nama_kk');
		$this->db->from('keluarga a');
		$this->db->join('warga b', 'a.kepala_keluarga = b.no_ktp');
	
		$this->db->where('a.id_keluarga', $id_keluarga);
		return $this->db->get();
	}

	function togrid_detail($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function get_detail_by_id($id_keluarga){
		$this->db->where('id_keluarga', $id_keluarga);
		return $this->db->get('keluarga');
	}

	#chart
	function getDataChartEkonomi($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('keluarga');
			$this->db->where('ekonomi', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartEkonomiSum($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('keluarga');
			$this->db->where('ekonomi', $d);
			
			$tmp += $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataGraph($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('keluarga');
		
		//$this->db->where('status', '1');
		
		return $this->db->get();
	}

	function getDataByktp($no_ktp){
		$this->db->flush_cache();
		$this->db->select('nama');
		$this->db->from('warga');
		$this->db->where('no_ktp', $no_ktp);

		return $this->db->get();
	}

	function getListPenduduk(){
		$this->db->flush_cache();
		$this->db->where('id_keluarga is null');
		$q = $this->input->post('q');
		// $q = isset($_POST['q']) ? $_POST['q'] : '';
		$this->db->like('nama', $q);

		return $this->db->get('warga');
	}

	//export
	function getDataKK(){
		$this->db->select('a.*,b.nama,c.nama_desa');
		$this->db->from('keluarga a');
		$this->db->join('warga b', 'b.no_ktp = a.kepala_keluarga');
		$this->db->join('desa c', 'c.id_desa = a.dusun');

		return $this->db->get();
	}

	function getAnggotaByKK($id_keluarga,$kepala_keluarga){
		$this->db->select('nama');
		$this->db->from('warga');
		$this->db->where('id_keluarga', $id_keluarga);
		$this->db->where('no_ktp !=', $kepala_keluarga);

		return $this->db->get();
	}
}

?>